package com.example.util;

/**
 * @author
 * @createtime 2021-08-12 15:25
 * @description
 */

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.javalite.http.Get;
import org.javalite.http.Http;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class JwdUtil {


    private static final String GOULD_KEY = "0c4be905989d9bc2186872ff232a04ff";

    /**
     * 根据地址返回经纬度
     * {xcoor=119.439735, ycoor=32.484554}
     *
     * @param address
     * @return
     */
    public static Map<String, String> getLonLat(String address) {

        Map<String, String> map1 = new HashMap<String, String>();
        Map<String, String> map2 = new HashMap<String, String>();
        try {
            Get get = Http.get("http://restapi.amap.com/v3/geocode/geo?key=0c4be905989d9bc2186872ff232a04ff&address=" + URLEncoder.encode(address, "utf-8"));
            String info = get.text();
//            System.out.println(info);
//            log.info(info);
            if (StringUtils.isNotBlank(info)) {
                JSONObject json = JSONObject.parseObject(info);
                List<Map<String, Object>> addressList = (List<Map<String, Object>>) json.get("geocodes");
//                System.out.println(addressList.get(0));
                if (!addressList.isEmpty()) {
                    String str = addressList.get(0).get("location").toString();
                    String[] location = str.split(",");

                    String xcoor = location[0];
                    String ycoor = location[1];


                    map1.put("xcoor", xcoor);
                    map1.put("ycoor", ycoor);
                    System.out.println(xcoor);
                    System.out.println(ycoor);


                    return map1;

                } else {
                    String xccor = "";
                    String yccor = "";
                    map2.put("", xccor);
                    map2.put("", yccor);
                    return map2;
                }

            }
        } catch (Exception e) {
//            log.error(e.getMessage());
        }
        return null;
    }


    public static String getLonLat1(String address) {
//    public static BigDecimal[] getLonLat1(String address) {

        Map<String, String> map1 = new HashMap<String, String>();
        Map<String, String> map2 = new HashMap<String, String>();
        try {
            Get get = Http.get("http://restapi.amap.com/v3/geocode/geo?key=0c4be905989d9bc2186872ff232a04ff&address=" + URLEncoder.encode(address, "utf-8"));
            String info = get.text();
//
            if (StringUtils.isNotBlank(info)) {
                JSONObject json = JSONObject.parseObject(info);
                List<Map<String, Object>> addressList = (List<Map<String, Object>>) json.get("geocodes");
//                System.out.println(addressList.get(0));
                if (!addressList.isEmpty()) {
                    String str = addressList.get(0).get("location").toString();

                    return str;

                }

            }
        } catch (Exception e) {
//            log.error(e.getMessage());
        }
        return null;
    }

    public static void main(String[] args) {
        JwdUtil zhjwdUtil = new JwdUtil();
        try {
            System.out.println(zhjwdUtil.getLonLat1("扬州市宝应县西安丰镇"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
